<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>C:\WorkingCopy\ColorTest\csvformat.pl.html</title>
<meta name="Generator" content="Vim/7.2">
<meta http-equiv="content-type" content="text/html; charset=Big5">
<style type="text/css">
<!--
.perlNumber { color: #80ac7b; background-color: #343434; }
.perlStringStartEnd { color: #b07050; background-color: #353535; }
.Constant { color: #b07050; }
.perlStatementFiledesc { color: #a2c090; background-color: #343434; }
.perlIdentifier { color: #90c0c0; }
.perlSpecialString { color: #dc966b; background-color: #343434; }
.perlOperator { color: #c0c090; background-color: #404040; }
.perlStatement { color: #c0c090; }
.perlRepeat { color: #c0b790; background-color: #343434; }
.Statement { color: #c0c090; }
pre { font-family: monospace; color: #cccccc; background-color: #343434; }
body { font-family: monospace; color: #cccccc; background-color: #343434; }
.perlSharpBang { color: #c0c090; background-color: #505050; }
.Comment { color: #647bcf; }
.perlVarPlain { color: #74c5c6; background-color: #343434; }
-->
</style>
</head>
<body>
<pre>
<span class="perlSharpBang">#!perl -w</span>

<span class="Comment"># Time-stamp: &lt;2002/04/06, 13:12:13 (EST), maverick, csvformat.pl&gt;</span>
<span class="Comment"># Two pass CSV file to table formatter</span>

<span class="perlVarPlain">$delim</span> = <span class="perlVarPlain">$#ARGV</span> &gt;= <span class="perlNumber">1</span> ? <span class="perlVarPlain">$ARGV</span>[<span class="perlNumber">1</span>] : <span class="perlStringStartEnd">'</span><span class="Constant">,</span><span class="perlStringStartEnd">'</span>;
<span class="perlStatementFiledesc">print</span> <span class="perlIdentifier">STDERR</span> <span class="perlStringStartEnd">&quot;</span><span class="Constant">Split pattern: </span><span class="perlVarPlain">$delim</span><span class="perlSpecialString">\n</span><span class="perlStringStartEnd">&quot;</span>;

<span class="Comment"># first pass</span>
<span class="perlStatementFiledesc">open</span> <span class="perlIdentifier">F</span>, <span class="perlStringStartEnd">&quot;</span><span class="Constant">&lt;</span><span class="perlVarPlain">$ARGV</span><span class="Constant">[0]</span><span class="perlStringStartEnd">&quot;</span> <span class="perlOperator">or</span> <span class="perlStatement">die</span>;
<span class="perlRepeat">while</span>(&lt;F&gt;)
{
  <span class="perlStatement">chomp</span>;
  <span class="perlVarPlain">$i</span> = <span class="perlNumber">0</span>;
  <span class="perlStatement">map</span> { <span class="perlVarPlain">$max</span>[<span class="perlVarPlain">$_</span>-&gt;[<span class="perlNumber">1</span>]] = <span class="perlVarPlain">$_</span>-&gt;[<span class="perlNumber">0</span>] <span class="Statement">if</span> <span class="perlVarPlain">$_</span>-&gt;[<span class="perlNumber">0</span>] &gt; (<span class="perlVarPlain">$max</span>[<span class="perlVarPlain">$_</span>-&gt;[<span class="perlNumber">1</span>]] || <span class="perlNumber">0</span>) }
    (<span class="perlStatement">map</span> {[<span class="perlStatement">length</span> <span class="perlVarPlain">$_</span>, <span class="perlVarPlain">$i</span>++]} <span class="perlStatement">split</span>(<span class="perlVarPlain">$delim</span>));
}
<span class="perlStatementFiledesc">close</span> <span class="perlIdentifier">F</span>;

<span class="perlStatementFiledesc">print</span> <span class="perlIdentifier">STDERR</span> <span class="perlStringStartEnd">'</span><span class="Constant">Field width:   </span><span class="perlStringStartEnd">'</span>, <span class="perlStatement">join</span>(<span class="perlStringStartEnd">'</span><span class="Constant">, </span><span class="perlStringStartEnd">'</span>, <span class="perlVarPlain">@max</span>), <span class="perlStringStartEnd">&quot;</span><span class="perlSpecialString">\n</span><span class="perlStringStartEnd">&quot;</span>;
<span class="perlStatementFiledesc">print</span> <span class="perlIdentifier">STDERR</span> <span class="perlStatement">join</span>(<span class="perlStringStartEnd">'</span><span class="Constant"> </span><span class="perlStringStartEnd">'</span>, <span class="perlStatement">map</span> {<span class="perlStringStartEnd">'</span><span class="Constant">-</span><span class="perlStringStartEnd">'</span> x <span class="perlVarPlain">$_</span>} <span class="perlVarPlain">@max</span>);

<span class="Comment"># second pass</span>
<span class="perlStatementFiledesc">open</span> <span class="perlIdentifier">F</span>, <span class="perlStringStartEnd">&quot;</span><span class="Constant">&lt;</span><span class="perlVarPlain">$ARGV</span><span class="Constant">[0]</span><span class="perlStringStartEnd">&quot;</span> <span class="perlOperator">or</span> <span class="perlStatement">die</span>;
<span class="perlRepeat">while</span>(&lt;F&gt;)
  {
  <span class="perlStatement">chomp</span>;
  <span class="perlVarPlain">$i</span> = <span class="perlNumber">0</span>;
  <span class="perlStatement">map</span> { <span class="perlStatementFiledesc">printf</span>(<span class="perlStringStartEnd">&quot;</span><span class="Constant">%-</span><span class="perlVarPlain">$max</span><span class="Constant">[</span><span class="perlVarPlain">$_</span><span class="Constant">-&gt;[1]]s </span><span class="perlStringStartEnd">&quot;</span>, <span class="perlVarPlain">$_</span>-&gt;[<span class="perlNumber">0</span>]) }
    (<span class="perlStatement">map</span> {[<span class="perlVarPlain">$_</span>, <span class="perlVarPlain">$i</span>++]} <span class="perlStatement">split</span>(<span class="perlVarPlain">$delim</span>));
  <span class="perlStatementFiledesc">print</span> <span class="perlStringStartEnd">&quot;</span><span class="perlSpecialString">\n</span><span class="perlStringStartEnd">&quot;</span>;
}
<span class="perlStatementFiledesc">close</span> <span class="perlIdentifier">F</span>;
</pre>
</body>
</html>
